package com.example.neusoft215gdh.androidpro_21.Model;
import com.example.neusoft215gdh.androidpro_21.Interface.ClubFace;
import com.example.neusoft215gdh.androidpro_21.JavaBean.ClubCourse;
import com.example.neusoft215gdh.androidpro_21.JavaBean.ClubDetail;
import com.example.neusoft215gdh.androidpro_21.JavaBean.ClubList;
import com.example.neusoft215gdh.androidpro_21.Listener.ClubListener;
import com.example.neusoft215gdh.androidpro_21.Listener.UserListenerr;
import com.example.neusoft215gdh.androidpro_21.Service.ClubService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
 * Created by Rainbow on 2017/9/30.
 */
public class ClubModel implements ClubFace{

    private Retrofit retrofit;
    ClubService clubService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址

   public ClubModel(){
       retrofit = new Retrofit.Builder()
               .baseUrl(BASE_URL)
               .addConverterFactory(GsonConverterFactory.create())//转换Json
               .build();
   }
//2、用Retrofit对象创建Service对象
//3、用Servivec创建Call对象
    @Override
    public void getClubMaxRe(String sessionId, int page,final ClubListener listener) {
        clubService=retrofit.create(ClubService.class);
           Call<List<ClubList>> call=clubService.getMaxRe(sessionId,page);
        call.enqueue(new Callback<List<ClubList>>() {
            @Override
            public void onResponse(Call<List<ClubList>> call, Response<List<ClubList>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }
            @Override
            public void onFailure(Call<List<ClubList>> call, Throwable t) {

            }
        });
    }

    @Override
    public void getClubZuiJin(String sessionId, int jingdu, int weidu, int city_id,final ClubListener listener) {
        clubService=retrofit.create(ClubService.class);
        Call<List<ClubList>> call=clubService.getZuiJin(sessionId,jingdu,weidu,city_id);
        call.enqueue(new Callback<List<ClubList>>() {
            @Override
            public void onResponse(Call<List<ClubList>> call, Response<List<ClubList>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<ClubList>> call, Throwable t) {

            }
        });
    }

    /**
     * 精品课程
     * @param sessionId
     * @param id
     * @param related
     * @param map
     * @param listener
     */
    @Override
    public void getClubDetail(String sessionId, String id, String related,String map,final ClubListener listener) {
            clubService=retrofit.create(ClubService.class);
            Call<ClubCourse> call=clubService.getClubDetail(sessionId,id,related,map);
            call.enqueue(new Callback<ClubCourse>() {
                @Override
                public void onResponse(Call<ClubCourse> call, Response<ClubCourse> response) {
                    if (response.isSuccessful() && response.body() != null) {
                        listener.onResponseClubDetailCourse(response.body());
                    } else {
                        listener.onFail("onresponse fail");
                    }
                }
                @Override
                public void onFailure(Call<ClubCourse> call, Throwable t) {

                }
            });
        }

    /**
     * 俱乐部简介
     * @param club_id
     * @param sessionid
     * @param listener
     */
    @Override
    public void getClubDetailjianjie(String club_id,String sessionid,final ClubListener listener) {
        clubService=retrofit.create(ClubService.class);
        Call<ClubDetail> call=clubService.getClubDetailjianjie(club_id,sessionid);
        call.enqueue(new Callback<ClubDetail>() {
            @Override
            public void onResponse(Call<ClubDetail> call, Response<ClubDetail> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponseClubDetailjianjie(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }
            @Override
            public void onFailure(Call<ClubDetail> call, Throwable t) {
            }
        });
    }

    //搜索俱乐部
    @Override
    public void getSearchClub(String club_name, final UserListenerr userListener) {
        clubService = retrofit.create(ClubService.class);
        Call<List<ClubList>> call = clubService.getSearchClub(club_name);
      call.enqueue(new Callback<List<ClubList>>() {
          @Override
          public void onResponse(Call<List<ClubList>> call, Response<List<ClubList>> response) {
              if (response.isSuccessful() && response.body() != null) {
                  userListener.onResponse(response.body());
              } else {
                  userListener.onFail("onresponse fail");
              }
          }

          @Override
          public void onFailure(Call<List<ClubList>> call, Throwable t) {

          }
      });
    }
}
